﻿using System;
using System.ComponentModel;
using Microsoft.WowAddonStudio.FrameXml.Serialization;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Descriptors.Filters
{
    public class ReadOnlyPropertyDescriptorFilter : PropertyDescriptorFilter
    {
        /// <summary>
        /// Filters the property descriptor on the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="propertyDescriptor">The property descriptor.</param>
        /// <returns>A property descriptor</returns>
        public override PropertyDescriptor Filter(object instance, PropertyDescriptor propertyDescriptor)
        {
            if (!(instance is IFrameXmlControl))
                throw new ArgumentException("Only FrameXML controls can be filtered.", "instance");

            var control = instance as IFrameXmlControl;
            var inheritableObject = control.SerializationObject as InheritableObject;
            if (inheritableObject != null)
                return TypeDescriptor.CreateProperty(propertyDescriptor.ComponentType, propertyDescriptor, new ReadOnlyAttribute(control.IsInherited));

            return propertyDescriptor;
        }
    }
}